spring schedule learning

  • 记录Spring schedule相关的学习资料
  • 其中主要注意cron的写法+配置文件怎么配置的问题

CRON expression

配置schedule

文档

完整spring配置文件demo

要注意schema+cron表达式写法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd 
                        http://www.springframework.org/schema/task
               http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- drs module schedule task -->
    <!-- 配置具体任务 -->
    <task:scheduled-tasks scheduler="baseScheduler">
        <!-- 第0min0sec开始,每隔10min跑一次 -->
        <task:scheduled ref="licenseModuleService"
            method="checkLicenses" cron="0 0/10 * * * *" />
        <!-- 0min0sec开始,每10min一次 -->
        <task:scheduled ref="monNotificationModuleService"
            method="checkMonNotification" cron="0/30 * * * * *" />
        <!-- 任意min0sec开始,每30min一次,具体参考cron配置吧,比较复杂 -->
        <task:scheduled ref="systemConfigModuleService"
            method="clearSystemLogs" cron="0 */30 * * * *" />
        <task:scheduled ref="securityPolicyModuleService"
            method="clearCacheByLockoutPolicy" cron="7 */1 * * * *" />
        <!-- fixed是周期,initial是开始时间,单位是毫秒 -->
        <task:scheduled ref="drsStrategyProcess"
            method="invoke" fixed-delay="600000" initial-delay="600000" />
        <!-- 固定15sec -->
        <task:scheduled ref="thaHtSender" method="send"
            fixed-rate="15000" />
    </task:scheduled-tasks>

    <task:scheduler id="baseScheduler" pool-size="10" />

    <!-- may be used in future -->
    <!-- 这里不配置,也可以运行,跟scheduler好像是重复的 -->
    <!-- <task:executor id="baseExecutor" pool-size="5" /> -->
    <!-- <task:annotation-driven executor="baseExecutor" scheduler="baseScheduler"/> -->
</beans>